const { toUrl, globalData } = getApp();
import { request } from "@/utils/request";

Page({
  toUrl,
  data: {
    menu: 0,
    type: "normal",
    list: [],
    page: 1,
    isEnd: false,
    count: 0,
    globalData,
  },
  select(e) {
    const { index } = e.currentTarget.dataset;
    const { select } = this.data.list[index];
    this.data.list.map((item) => {
      item.select = false;
    });
    this.data.list[index].select = !select;
    this.setData({
      list: this.data.list,
    });
  },
  apply(e) {
    console.log(e);
    let that = this;

    let { integral, money, receive_type,coupon_id } = e.currentTarget.dataset;
    if (receive_type == 2) {
      wx.showModal({
        content: `是否用${integral}积分兑换 \r\n ${money}元停车券?`,
        confirmColor: "#1D2A73",
        cancelColor: "#666",
        confirmText: "确定",
        success(res) {
          if (res.confirm) {
            that.getApply(coupon_id)
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    }else{
      that.getApply(coupon_id)
    }
  
  },

  getApply(coupon_id) {
    let that = this;
    let data = {
      coupon_id: coupon_id,
    };
    request("coupon/apply", { data }).then((res) => {
      if (res.errcode == 1) {
        wx.showToast({
          title: "获取成功",
        });
        that.setData({
          page: 1,
        });
        setTimeout(() => {
          that.getDetail();
        }, 2000);
      } else {
        wx.showToast({
          title: res.message,
        });
      }
    });
  },

  getDetail() {
    console.log(this.data.page, this.data.count);
    if (this.data.page > 1 && this.data.page > this.data.count) {
      this.setData({
        isEnd: true,
      });
      return;
    }
    let data = {
      typeid: 2,
      type: 0,
      page: this.data.page,
      suppliers_id: "",
    };
    wx.showLoading();
    request("coupon/couponlist", { data }).then((res) => {
      wx.hideLoading();
      if (res.errcode == 1) {
        // res.data = [
        //   {
        //     coupon_id: 26,
        //     name: "免费满减",
        //     end_time: "2023.08.13",
        //     money: "10.00",
        //     receive_type: 1,
        //     integral: 0,
        //     status: 2,
        //     description: "免费领取",
        //   },
        //   {
        //     coupon_id: 26,
        //     name: "免费满减",
        //     end_time: "2023.08.13",
        //     money: "10.00",
        //     receive_type: 2,
        //     integral: 0,
        //     status: 2,
        //     description: "免费领取",
        //   },
        // ];
        let list = this.data.list;
        if (this.data.page > 1) {
          res.data.array.forEach((it) => {
            list.push(it);
          });
          this.setData({
            list,
            count: res.data.count,
            page: ++this.data.page,
          });
        } else {
          this.setData({
            list: res.data,
            count: res.data.count,
            page: ++this.data.page,
          });
        }
      } else {
        wx.showToast({
          title: res.message,
        });
      }
    });
  },
  onLoad({ type }) {
    console.log(type);
    if (type) {
      this.setData({
        type,
      });
    }
    this.getDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let isEnd = this.data.isEnd;
    if (isEnd) {
      return;
    } else {
      this.getDatail();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
